package com.hrt.web.product.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.GiftInfor;
import com.hrt.web.product.dao.GiftInforDao;

@Repository
public class GiftInforDaowebImpl extends BaseDaoImpl<GiftInfor> implements GiftInforDao {

	/**
	 * 前台的礼品展示列表
	 * */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Override
	public List<GiftInfor> findofeight() {
//		String Hql = "select distinct  FROM GiftInfor WHERE isDelete=0 and ispush=1 ORDER BY id DESC";
		//有分类的礼品基本信息才查找出来
		String Hql = "select distinct a.giftInfor from GiftTypeDir as a where a.giftInfor.ispush=1 and a.giftInfor.isdelete=0 ORDER BY a.giftInfor.id DESC";
		Query query = this.getSession().createQuery(Hql);
		query.setFirstResult(0);
		query.setMaxResults(8);
		return query.list();
	}
	
	/**分页*/
	@SuppressWarnings({ "deprecation", "rawtypes" })
	@Override
	public Long totalCount() {
		final String hql="select count(*) from GiftInfor where ispush=:ispush and isdelete=:isdelete ORDER BY id DESC";
		List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){
			@Override
			public List doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery(hql);
				
				query.setInteger("isdelete", 0);
				query.setInteger("ispush", 1);
				return (List) query.list();
			}
		});
		return new Long(list.get(0).toString());
	}
	@SuppressWarnings({ "unchecked", "deprecation", "rawtypes" })
	@Override
	public List queryByPage(final int pageIndex, final int pageSize)  {
		List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){
			//有分类的礼品基本信息才查找出来
			String hql="select distinct a.giftInfor from GiftTypeDir as a where a.giftInfor.ispush=:ispush and a.giftInfor.isdelete=:isdelete ORDER BY a.giftInfor.id DESC";
			@Override
			public List doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery(hql);
				query.setInteger("ispush", 1);
				query.setInteger("isdelete", 0);
				query.setFirstResult((pageIndex-1)*pageSize);
				query.setMaxResults(pageSize);
				List<?> result=query.list();
				return  (List) result;
			}
		});
		return list;
	
	}
	
	@SuppressWarnings({ "unchecked", "deprecation", "rawtypes" })
	public List queryByPage(final String hql,final int pageIndex, final int pageSize)  {
		List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){
			@Override
			public List doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery(hql);
				query.setInteger("ispush", 1);
				query.setInteger("isdelete", 0);
				query.setFirstResult((pageIndex-1)*pageSize);
				query.setMaxResults(pageSize);
				List<?> result=query.list();
				return  (List) result;
			}
		});
		return list;
	}
	
	/**分页查询的重写*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<GiftInfor> queryPageByGiftId(List conditions,
			int pageIndex, int pageSize) {
		String hql = "select detail from GiftInfor detail";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where detail.ispush=1 and detail." + field + " in(" + val+")";
			} else {
				hql = hql + " and detail." + field + "=" + val+" ORDER BY id DESC";
			}
		}
		List<GiftInfor> results = super.queryByPage(hql, pageIndex, pageSize);
		return results;
	}
}
